import { combineReducers } from 'redux';
import {NavigationActions} from 'react-navigation'
import  {RootNavigator } from '../../AppNavigator'
import userReducer from './userReducer';

console.log("rootNavigator:",RootNavigator);
// Start with two routes: The Main screen, with the Login screen on top.
const firstAction = RootNavigator.router.getActionForPathAndParams('main');
console.log("firstAction:",firstAction);//action:{type: "Navigation/NAVIGATE", routeName: "IndexA"}routeName:"main"type:"Navigation/NAVIGATE"
const tempNavState = RootNavigator.router.getStateForAction(firstAction);
console.log("tempNavState:",tempNavState);//输出是{index:0isTransitioning:falsekey:"StackRouterRoot",routes:[index: 0isTransitioning: falsekey: "StackRouterRoot"routes: {params: undefined, routes: Array(4), index: 0, isTransitioning: false, key: "id-1530674251434-4"}}
const secondAction = RootNavigator.router.getActionForPathAndParams('LoginActivity');
console.log("secondAction:",secondAction);//输出是{type: "Navigation/NAVIGATE", routeName: "Login"}
const initialNavState = RootNavigator.router.getStateForAction(
    secondAction,
    tempNavState
);
console.log("initialNavState:",initialNavState);
//输出{key: "StackRouterRoot", isTransitioning: true, index: 1, routes: [{params: undefined, key: "id-1530603317101-0", routeName: "Main"},{params: undefined, routeName: "Login", key: "id-1530603317101-1"}]}
function nav(state = initialNavState, action) {
    let nextState;
    switch (action.type) {
        case 'Login':
            nextState = RootNavigator.router.getStateForAction(
                NavigationActions.back(),
                state
            );
            break;
        case 'Logout':
            nextState = RootNavigator.router.getStateForAction(
                NavigationActions.navigate({ routeName: 'LoginActivity' }),
                state
            );
            break;
        default:
            nextState = RootNavigator.router.getStateForAction(action, state);
            break;
    }

    // Simply return the original `state` if `nextState` is null or undefined.
    return nextState || state;
}


const initialAuthState = { isLoggedIn: false };

function auth(state = initialAuthState, action) {
    switch (action.type) {
        case 'Login':
            return { ...state, isLoggedIn: true };
        case 'Logout':
            return { ...state, isLoggedIn: false };
        default:
            return state;
    }
}

const AppReducer = combineReducers({
    nav,
    auth,
    userStore: userReducer
});
//combineReducers是将应用的state进行组合。
export default AppReducer;